<?php
/**
 * 试卷接口
 * 
 * @author river_he@eueang.com
 */
final class Gos_Controller_Exam extends Gospel_Controller_Api {
	/**
	 * 分发action之前
	 * 
	 * @param Gospel_Http_Request_Abstract $request
	 * @return void
	 */
	protected function beforeDespatch(Gospel_Http_Request_Abstract &$request){
		$act = $request->getAction();
		if(is_numeric($act)){
			$request->setItem('act', 'info');
			$request->setItem('cid', $act);
		}
	}
	
	/**
	 * 首页资源没有
	 */
	public function indexAction(){
		header('http/1.1 404 NOT FOUND');
	}
	
	/**
	 * 添加记录
	 * 
	 * @param array $exam = array(
	 * );
	 * @return int
	 */
	public function append(array $exam){
		
	}
	
	/** 
	 * 记录详细信息
	 * 
	 * @throws Gospel_Controller_Api_Exception
	 * @see index.php?control=exam&act=1
	 */
	public function infoAction(){
		$eid = $this->getRequest()->getInt('eid');
		if($eid > 0){
			$module = new Gos_Model_Exam();
			$exam = $module->getItem('nId='.$eid, true);
			if($exam){
				throw new Gospel_Controller_Api_Exception(200, 'OK', $exam);
			}else{
				throw new Gospel_Controller_Api_Exception(404, 'the exam paper not found');
			}
		}else{
			throw new Gospel_Controller_Api_Exception(101, 'arguments eid is error');
		}
	}
	
	/**
	 * 记录列表
	 * 
	 * @throws Gospel_Controller_Api_Exception
	 * @see index.php?control=exam&act=list&q={}
	 */
	public function listAction(){
		$url = 'index.php?control=exam&act=list&q=';
		$array = array();
		$where = array('nStat=');
		$query = $this->getRequest()->getString('q');
		
		$where = $where!=''?json_decode($where):'1=1';
		$end = $end>0?$end:10;
		$sort = $sort!=''?str_replace('_', ' ', $sort):'nId DESC';
		$exaModule = new Gos_Model_Exam();
		$exams = $exaModule->getItems($where, $sort, $offset, $end);
		throw new Gospel_Controller_Api_Exception(200, 'OK', $exams);
	}
}
//end